import {cbs, pwdReg} from '../../../../config/settings.js';
import Withdraw from '../../../Common/Withdraw/';
import md5 from 'js-md5';
import {mapGetters} from 'vuex';

module.exports ={
	data() {
		return {
			userData: [],
			deliverData: [],
			page: 1,
			size: 20,
			type: 1,          //订单类型 1-用户 2-送水工
			total: 0,
			status: 3,
			withdrawVisible: false,
			tipsVisible: false,
			payForm: {
				pwd: ''
			},
			payRules: {
				pwd: [{
					validator: (rule, value, cb) => {
						if(!value) {
							cb(new Error('请输入支付密码'))
						}else if(!pwdReg.test(value)) {
							cb(new Error('密码长度需在6-16位之间，同时包含数字和字母'))
						}else {
							cb()
						}
					},
					trigger: 'blur'
				}]
			},
			depositId: '',
			deposit: 0,
			balance: 0,
		}
	},
	components: {
		Withdraw
	},
	methods: {
		/**
	     * 获取提现信息
	     * 1-用户 2-送水工 3-水店
	     */
		getList(type=1, page=1, status=this.status) {
			this.type = type;
			let obj = {
				requestPage: page,
				pageSize: this.size
			};

			if(status < 3) {
				obj.status = status;
			}
			console.log(data)

			let data = this.$$formData(obj);

			switch(type) {
				case 1:
					this.getUserList(data);
					break;
				case 2:
					this.getDeliverList(data);
					break;
				default:
					this.getUserList(data);
			}

		},
		/**
	     * 获取用户押金列表
	     */
		getUserList(data) {
			this.$$api_order_userDepositOrders(data, res => {
				if(res.event === 0) {
					this.userData = res.obj;
					this.total = res.maxCount;
					console.log(res.obj)
				}else {
					cbs.submitError.call(this, `获取信息失败，${res.msg}`);
				}
			})
		},
		/**
	     * 获取送水工押金列表
	     */
		getDeliverList(data) {
			this.$$api_order_deliverDepositOrders(data, res => {
				if(res.event === 0) {
					this.deliverData = res.obj;
					this.total = res.maxCount;
				}else {
					cbs.submitError.call(this, `获取信息失败，${res.msg}`);
				}
			})
		},
		/**
	     * 改变当前页数
	     */
		handlePageChange(val) {
			this.page = val;
			this.getList(this.type, this.page);
		},
		/**
	     * 切换列表
	     */
		handleChange(v) {
			this.page = 1;
			this.getList(this.type, this.page)
		},
		/**
	     * 输入密码
	     */
		postPwd() {
			this.$refs.payForm.validate(valid => {
				if(valid) {
					this.getBalance(() => {
						if(this.deposit <= this.balance) {
							this.type === 1 ? this.handleUserReceive() : this.handleDeliverReceive()
						}else {
							cbs.submitError.call(this, `您的余额不足`);
						}
					});
				}
			})
		},
		/**
	     * 是否已设置支付密码
	     */
		isHasPayPwd() {
			this.hasPayPwd ? this.withdrawVisible = true : this.tipsVisible = true;
		},
 		/**
	     * 处理送水工押金订单
	     */
		handleDeliver(item, type) {
			this.depositId = item.waterDeliverDepositOrderId;
			this.deposit = item.deposit;
			type === 1 ? this.isHasPayPwd() : this.handleDeliverReject();
		},
		/**
	     * 接受送水工押金订单
	     */
		handleDeliverReceive() {
			let data = this.$$formData({
				waterDeliverDepositOrderId: this.depositId,
				receive: 1,
				payPwd: md5(this.payForm.pwd)
			})

			this.$$api_order_hangdleDeliverDeposit(data, res => {
				if(res.event === 0) {
					this.withdrawVisible = false;
					this.payForm.pwd = '';
					this.$store.dispatch('update_deposite_orders', --this.deposite);
					this.getList(2, this.page, this.status);
				}else if(res.event === 1003) {
					cbs.submitError.call(this, `支付密码错误`);
				}else {
					cbs.submitError.call(this, `获取信息失败，${res.msg}`);
				}
			})
		},
		/**
	     * 拒绝送水工押金订单
	     */
		handleDeliverReject() {
			let data = this.$$formData({
				waterDeliverDepositOrderId: this.depositId,
				receive: 2
			})
			this.$$api_order_hangdleDeliverDeposit(data, res => {
				if(res.event === 0) {
					this.$store.dispatch('update_deposite_orders', --this.deposite);
					this.getList(2, this.page, this.status);
				}else {
					cbs.submitError.call(this, `获取信息失败，${res.msg}`);
				}
			})
		},
		/**
	     * 处理用户押金订单
	     */
		handleUser(item, type) {
			this.depositId = item.userDepositOrderId;
			this.deposit = item.deposit;
			type === 1 ? this.isHasPayPwd() : this.handleUserReject();
		},
		/**
	     * 接受用户押金订单
	     */
		handleUserReceive() {
			let data = this.$$formData({
				userDepositOrderId: this.depositId,
				receive: 1,
				payPwd: md5(this.payForm.pwd)
			})

			this.$$api_order_hangdleUserDeposit(data, res => {
				if(res.event === 0) {
					this.withdrawVisible = false;
					this.payForm.pwd = '';
					this.$store.dispatch('update_deposite_orders', --this.deposite);
					this.getList(1, this.page, this.status);
				}else if(res.event === 1003) {
					cbs.submitError.call(this, `支付密码错误`);
				}else {
					cbs.submitError.call(this, `获取信息失败，${res.msg}`);
				}
			})
		},
		/**
	     * 拒绝用户押金订单
	     */
		handleUserReject() {
			let data = this.$$formData({
				userDepositOrderId: this.depositId,
				receive: 2
			})
			this.$$api_order_hangdleUserDeposit(data, res => {
				if(res.event === 0) {
					this.$store.dispatch('update_deposite_orders', --this.deposite);
					this.getList(1, this.page, this.status);
				}else {
					cbs.submitError.call(this, `获取信息失败，${res.msg}`);
				}
			})
		},
		/**
	     * 获取余额
	     */
		getBalance(cb) {
			this.$$api_manager_balance(null, res => {
		        if(res.event === 0) {
		        	this.balance = res.obj;
		        	cb();
		        }else {
		            cbs.submitError.call(this, `获取余额信息失败，${res.msg}`);
	            }
	        })
		}
	},
	created() {
		this.getList();
	},
	computed: {
        hasPayPwd() {
          return this.$store.state.user.userinfo.hasPay;
        },
        ...mapGetters({
            deposite: 'getDepositeOrders'
        }),
    }
}